<?php
/**
 * Orderdetail_model 订单详情模型
 * @author  yw 
 *  2017年6月6日17:32:26
 */
class Orderdetail_model extends BS_Model
{
    /**
     * [setOrderDetailBySuiteCode 更新订单(添加套装码)]
     * @param [type] $orderidDetailId [订单id]
     * @param [type] $suite_code      [套装码]
     */
    public function updateOrderDetailBySuiteCode($orderidDetailId, $suite_code)
    {
    	$this->db->from('order_detail');
      	$where  = array('order_detail_id'=>$orderidDetailId,'status !='=>2);
      	$this->db->where($where);
      	$query = $this->db->get();
       
      	if($query->num_rows() > 0)
      	{
        	$data = array('suite_code'=>$suite_code,'status'=>2,'update_time'=>time());
        	$this->db->update('order_detail',$data,array('order_detail_id'=>$orderidDetailId));
        	$number = $this->db->affected_rows();
        	return $number;
      	}
        return null;
    }
   
   
    public function getOrderDetail($param = array())
    {
      	$this->db->select('A.order_detail_id,A.order_id,A.price,A.amount,A.goods_id,B.name,B.banner_img,B.other_name');
      	$this->db->from('order_detail AS A');
      	$this->db->where_in('order_id', $param);
      	$this->db->join('goods AS B','B.goods_id = A.goods_id' ,'left');
      	$query = $this->db->get();
      	return $query->result_array();
	}

  /**
   * [getOrderByOrderid 根据订单id查询订单详情]
   * @param  [type] $order_id [订单id]
   * @return [type]           [description]
   */
    public function getOrderByOrderid($order_id)
    {
        $this->db->select('A.order_detail_id,A.order_id,A.status,A.amount,A.suite_code,A.goods_id,A.activity_id,A.price as detailprice,goods.name');
        $this->db->from('order_detail AS A');
        $this->db->where('order_id', $order_id);
        $this->db->join('goods','goods.goods_id = A.goods_id' ,'left');
        $query = $this->db->get();
        return $query->result_array();
    }

    public function getOrderDetailByOrderid($order_id)
    {
        $query = $this->db->from('order_detail as A')
                            ->select('A.*,G.name,G.banner_img,G.other_name,G.activity_id')
                            ->join('goods as G','on G.goods_id = A.goods_id')
                            ->where('A.order_id',$order_id)
                            ->get();
        return $query->num_rows() > 0 ?  $query->result_array() : array();
    }
    /**
     * [getOrderInfoById 查询订单状态]
     * @param  [type] $id [订单详情id]
     * @return [type]     [全部发货完成返回订单id 否则返回false 未查询到订单返回空数组]
    */
    public function getOrderInfoById($id)
    {
       	$query = $this->db->from('order_detail as A')->select('A.order_id')->where('order_detail_id',$id)->get();
	    if($query->num_rows()>0)
	    {
	       	$data = $query->row_array();
	        //查询订单的数量
	        $where = array('order_id'=>$data['order_id'],'status !='=>2);
	        $query1 = $this->db->from('order_detail')->where($where)->get();
	        if($query1->num_rows()==0)
	    	{
	    	    return $data['order_id'];
	    	}
	    	return false;
	    }
	    return array();
    }

    /**
     * 插入一条订单详情
     * @param array $order_detail 订单详情记录
     * @return  新插入行的id ,插入失败返回false
    */
    public function addOrderDetail($order_detail)
    {
        $query = $this->db->insert('order_detail', $order_detail);
        if($query === false)
        {
          return false;
        }
        return $this->db->insert_id();
    }

    /**
     * 根据order_id删除订单详情
     * @param  int $order_id 订单id
     * @return bool     删除成功返回true,失败返回false
    */
    public function delOrderDetailByOrderId($order_id)
    {
        $this->db->where('order_id', $order_id);
        $result = $this->db->delete('order_detail');
        return $result;
    }
    /**
     * 批量插入订单详情数据
     * @param  array[array[]] $data 订单详情数据
     * @return Number of rows inserted or FALSE on failure
    */
    public function insertOrderDetailBatch($data)
    {
        $result = $this->db->insert_batch('order_detail', $data);
        return $result;
    }

    /**
     * 根据订单id和参数数组更新订单详情
     * @param  int $order_id 订单号
     * @param  array $arr      参数数组(键值对)
     * @return bool           成功返回true,失败返回false
     */
    public function updateOrderDetailByOrderid($order_id, $arr)
    {
    	$this->db->set($arr);
        $this->db->where('order_id', $order_id);
        $result = $this->db->update('order_detail');
        return $result;
    }
    /**
     * [updateOrderDetailStatusByOrderId 修改订单详情的状态]
     * @param  array  $order_id [订单id]
     * @return [type]           [受影响的行数]
    */
    public function updateOrderDetailStatusByOrderId($order_id = array())
    {
        $arr = array('status'=>4,'update_time'=>time());
        $this->db->set($arr);
        $this->db->where_in('order_id',$order_id);
        $this->db->update('order_detail');
        return $this->db->affected_rows();
    }

    /**
     * 根据订单详情id获取订单详情
     * @param  [type] $order_detail_id [description]
     * @return array                  [description]
     */
    public function getOrderDetailByOrderDetailId($order_detail_id)
    {
        $query = $this->db->select('*')
                 ->from('order_detail')
                 ->where('order_detail_id', $order_detail_id)
                 ->get();
        if($query->num_rows() > 0)
        {
            return $query->row_array();
        }
        return null;
    }

    /**
     * 根据订单详情id和参数数组更新订单详情
     * @param  int $order_detail_id 订单详情号
     * @param  array $arr      参数数组(键值对)
     * @return bool           成功返回true,失败返回false
     */
    public function updateOrderDetailByorderDetailId($order_detail_id, $arr)
    {
        $this->db->set($arr);
        $this->db->where('order_detail_id', $order_detail_id);
        $result = $this->db->update('order_detail');
        return $result;
    }
}